//
//  ATRAZ500MapCollectionAction.m
//  ABCTime
//
//  Created by lianglibao on 2018/5/25.
//  Copyright © 2018年 100TAL. All rights reserved.
//

#import "ATRAZ500MapCollectionAction.h"
#import "ATRAZ500MapCollectionActionCell.h"
#import "ATRAZMapCardModel.h"
#import "ATUtil.h"

NSString * const kATRazMapHomeBgm = @"raz_map_home_bgm.mp3";

@interface ATRAZ500MapCollectionAction ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, ATRAZ500MapCollectionActionCellDelegate>

@property (strong, nonatomic) UICollectionView *pCollectionView;
@end

static NSString *const CELL_ID = @"ATRAZ500MapCollectionActionCell";

@implementation ATRAZ500MapCollectionAction

- (UICollectionView *)pCollectionView {
    
    if (!_pCollectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
        layout.itemSize = CGSizeMake(ATSCREEN_WIDTH(), ATSCREEN_HEIGHT());
        
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _pCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        
        _pCollectionView.dataSource = self;
        
        _pCollectionView.delegate = self;
        
        _pCollectionView.backgroundColor = [UIColor clearColor];
        
        [_pCollectionView registerClass:ATRAZ500MapCollectionActionCell.class forCellWithReuseIdentifier:CELL_ID];
        
        _pCollectionView.showsHorizontalScrollIndicator = NO;
        
        _pCollectionView.bounces = NO;
        
    }
    
    return _pCollectionView;
}

// MARK: - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ATRAZ500MapCollectionActionCell *razCell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
    razCell.deleagte = self;
    razCell.cardModel = self.dataSource[indexPath.row];
    return razCell;
    
}

// MARK: - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    ATRAZMapCardModel *model =  self.dataSource[indexPath.row];
    return CGSizeMake(model.subjectWidth, ATSCREEN_HEIGHT());

}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([cell isKindOfClass:ATRAZ500MapCollectionActionCell.class]) {
        // stop cell's animationView
    
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        return nil;
    
    }
    
    return nil;
}

// MARK: - public
- (UICollectionView *)collectionView {
    
    return self.pCollectionView;
}

- (void)setDataSource:(NSArray *)dataSource {
    
    if (_dataSource != dataSource) {
        
        _dataSource = dataSource;
    }
    
    [self.pCollectionView reloadData];
}

- (void) scrollAtIndex:(NSInteger)index {
    
    if (index < self.dataSource.count) {
        
        [self.pCollectionView reloadData];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        
        [self.pCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:false];
    }
}

- (void)cardButtonClickedButton:(UIButton *)button withIndex:(NSInteger)index {
    if ([self.delegate respondsToSelector:@selector(razMapCollectionActionWithClickedButton:atIndex:)]) {
        [self.delegate razMapCollectionActionWithClickedButton:button atIndex:index];
    }

}

@end
